<template>
  <div class="navbar">
    <div class="nav-container">
      <div class="nav-left">
        <router-link to="/" class="logo">
          <el-icon><Reading /></el-icon>
          <span>书海阁</span>
        </router-link>
        
        <el-menu
          :default-active="$route.path"
          mode="horizontal"
          router
          class="nav-menu"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/books">图书</el-menu-item>
          <el-menu-item index="/about">关于我们</el-menu-item>
        </el-menu>
      </div>

      <div class="nav-right">
        <el-input
          v-model="searchQuery"
          placeholder="搜索图书..."
          class="search-input"
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>

        <el-button
          v-if="!isLoggedIn"
          type="primary"
          @click="$router.push('/login')"
        >
          登录
        </el-button>

        <template v-else>
          <el-badge :value="cartCount" class="cart-badge">
            <el-button @click="$router.push('/cart')" circle>
              <el-icon><ShoppingCart /></el-icon>
            </el-button>
          </el-badge>

          <el-dropdown>
            <el-button circle>
              <el-icon><User /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$router.push('/orders')">
                  我的订单
                </el-dropdown-item>
                <el-dropdown-item @click="$router.push('/profile')">
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item 
                  v-if="isAdmin" 
                  @click="$router.push('/admin')"
                  divided
                >
                  商家后台
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const searchQuery = ref('')
const cartCount = ref(3) // 模拟购物车数量

const isLoggedIn = computed(() => userStore.isLoggedIn)
const isAdmin = computed(() => userStore.isAdmin)

const handleSearch = () => {
  if (searchQuery.value.trim()) {
    router.push(`/books?search=${encodeURIComponent(searchQuery.value)}`)
  }
}

const handleLogout = async () => {
  await userStore.logout()
  router.push('/')
}
</script>

<style scoped>
.navbar {
  background: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 40px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
  text-decoration: none;
}

.nav-menu {
  border-bottom: none;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.search-input {
  width: 300px;
}

.cart-badge {
  margin-right: 8px;
}
</style>
